package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import DataObjects.Bon;
import DataObjects.ConstantObjects;
import DataObjects.Menu;
import DataObjects.MetaDataOrder;
import DataObjects.Recipe;

public class PfPtihatShulhanRecipesToChoose extends JPanel{

	private Dimension screenSize;
	
	private MainMenu mainMenu;
	private Menu menu;
	private Vector<Bon> bones;
	private Vector<Menu> menues;
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel bonPanel;
	private JPanel buttonPanel;
	private JPanel recipesPanel;
	private JPanel menuButtonsPanel;
	private MetaDataOrder metaDataOrder;
	private int x;
	private int y;
	private PfPtihatShulhanRecipesToChoose itemToChoosePanel;
	
	
	public PfPtihatShulhanRecipesToChoose (MainMenu mm,Vector<Menu> menues ,Menu m,Vector<Bon> vector, MetaDataOrder meta)
	{
		super();
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		this.mainMenu=mm;
		this.menues=menues;
		this.menu=m;
		this.bones=vector;
		this.metaDataOrder=meta;
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		itemToChoosePanel=this;
		initialize();
	}
	
	private void initialize()
	{	
		add(getLeftPanel());  		
		add(getRightPanel());	
	}

	
	
	private JPanel getLeftPanel()            //search by category panel and choose specific item panel 
	{		
		leftPanel=new JPanel();
		leftPanel.setBackground(new Color(248, 248, 255));
		leftPanel.setBorder(new TitledBorder(null, "BON ", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		BonPanel bonPanel=new BonPanel(bones,itemToChoosePanel);
		
		JScrollPane scroll=new JScrollPane(bonPanel);
		scroll.setPreferredSize(new Dimension(580, 550));
		leftPanel.add(scroll);
		
		return leftPanel;
	}
	
	
	
	private JPanel getRightPanel()           
	{
		
		rightPanel=new JPanel();
		rightPanel.setBorder(new TitledBorder(null, "MENU ", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
		rightPanel.add(getMenuButtonsPanel());
		rightPanel.add(getMenuRecipesPanel());
		return rightPanel;
	}
	
	
	private JPanel getMenuRecipesPanel()            
	{
		recipesPanel = new JPanel();
		recipesPanel.setBorder(new TitledBorder(null, "מנות לבחירה", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		//GridLayout grid =new GridLayout(0, 5, 10, 20);
		//recipesPanel.setLayout(grid);
		recipesPanel.setPreferredSize(new Dimension(x/3, y/2));
		recipesPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5 ));
		ConstantObjects consobj=new ConstantObjects();
		String [] categories=consobj.getMenuCategories();
		for(int i=0; i<menu.getRecipes().size();i++)
		{		
			if(menu.getRecipes().get(i).getIsSelectable()==1)
			recipesPanel.add(getRecipesButton(menu.getRecipes().get(i)));
		}
		return recipesPanel;
	}
	
	
	
	
	
	
	private JButton getRecipesButton(Recipe r)
	{
		final Recipe recipe=r;
		JButton catButt=new JButton(recipe.getName());
		catButt.setPreferredSize(new Dimension(x/10,y/9));
		//catButt.setBackground(new Color(180,216,230));
		
		catButt.setFont(new Font("Tahoma", Font.BOLD, 12));
		catButt.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{			
				JButton but=(JButton) ebutt.getSource();
				Bon bon=new Bon();
				bon.setEid(metaDataOrder.getEid());
				bon.setMid(menu.getId());
				bon.setMname(menu.getName());
				if(menu.getType().equals("simple"))
					bon.setPrice(recipe.getPrice());
				if(menu.getType().equals("business"))
					bon.setPrice(menu.getPrice());
				bon.setRid(recipe.getId());
				bon.setRname(recipe.getName());
				bon.setTid(metaDataOrder.getTid());
				bon.setType(menu.getType());
				bones.add(bon);
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfPtihatShulhanLeftRigtPanels(mainMenu,menues,bones,metaDataOrder), BorderLayout.CENTER);					
				mainMenu.getContentPane().validate();
				
			}
		});
		return catButt;
	}
	
	
	
	private JPanel getMenuButtonsPanel()
	{
		menuButtonsPanel = new JPanel();
		menuButtonsPanel.setPreferredSize(new Dimension(x/3, y/4));
		menuButtonsPanel.setBackground(new Color(238, 232, 170));
		menuButtonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5,5 ));
		
		/*
		JButton butSave = new JButton("שמור");
		butSave.setPreferredSize(new Dimension(x/10,y/9));
		butSave.setBackground(new Color(180,216,0));		
		butSave.setFont(new Font("Tahoma", Font.BOLD, 15));
		butSave.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				
				
			}
		});
		
		*/
		
		
		JButton butCancel=new JButton("ביטול");
		butCancel.setPreferredSize(new Dimension(x/10,y/9));
		//butCancel.setBackground(new Color(180,216,0));		
		butCancel.setFont(new Font("Tahoma", Font.BOLD, 15));
		butCancel.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfScheem_All_Tables(mainMenu),BorderLayout.CENTER);
				mainMenu.getContentPane().validate();	
			}
		});
		
		JButton butReturn = new JButton("לבחירת תפריט");
		butReturn.setPreferredSize(new Dimension(x/10,y/9));
		//butReturn.setBackground(new Color(180,216,0));		
		butReturn.setFont(new Font("Tahoma", Font.BOLD, 15));
		butReturn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfPtihatShulhanLeftRigtPanels(mainMenu,menues,bones,metaDataOrder), BorderLayout.CENTER);					
				mainMenu.getContentPane().validate();				
			}
		});
		
		
		menuButtonsPanel.add(butCancel);
		menuButtonsPanel.add(butReturn);
		
		return menuButtonsPanel;
	}

	public Vector<Bon> getBones() {
		return bones;
	}

	public void setBones(Vector<Bon> bones) {
		this.bones = bones;
		mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
		mainMenu.getContentPane().add(new PfPtihatShulhanLeftRigtPanels(mainMenu,menues,bones,metaDataOrder), BorderLayout.CENTER);					
		mainMenu.getContentPane().validate();
		
	}
	
	
	
	
	
	
	
}
